﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using SDLITS.Common;
using SDLITS.Model;
using FineUI;
using MySoft.Data;


namespace SDLITS.Web.SystemManage
{
    public partial class RoleChange : System.Web.UI.Page//CommonBase
    {
        List<SDLITS.Model.Menu> areall = SDDbSession.Default.From<SDLITS.Model.Menu>().Where(SDLITS.Model.Menu._.IsShow == true).ToList();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindTree();
                BindData();
            }
        }
        protected void BindData()
        {
            int id = 0;
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                id = int.Parse(Request["id"]);
            }
            if (id > 0)
            {
                Role item = SDDbSession.Default.From<Role>().Where(Role._.RoleID == id).ToFirst();
                List<RoleInMenu> lists = SDDbSession.Default.From<RoleInMenu>().Where(RoleInMenu._.RoleID == id).ToList();
                txt_Descript.Text = item.Description;
                txt_Name.Text = item.RoleName;
            }
        }
        private void BindTree()
        {
            string menus = "";
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                List<RoleInMenu> menulist = SDDbSession.Default.From<RoleInMenu>().Where(RoleInMenu._.RoleID == Request["id"]).ToList();
                foreach (RoleInMenu row in menulist)
                {
                    menus += "[" + row.MenuID + "]";
                }
            }
            TreeNode n1 = new TreeNode();
            n1.Text = "系统功能操作权限";
            n1.NodeID = "0";
            n1.EnableCheckBox = true;
            n1.EnableCheckEvent = true;
            n1.Expanded = true;
            treeMenu.Nodes.Insert(0, n1);

            List<SDLITS.Model.Menu> tops = areall.FindAll(p => p.ParentID == 0);
            foreach (SDLITS.Model.Menu row in tops)
            {
                int childs = areall.FindAll(p => p.ParentID == row.MenuID).Count;
                TreeNode node = new TreeNode();
                node.Text = row.MenuName;
                node.NodeID = row.MenuID.ToString();
                node.EnableCheckBox = true;
                node.EnableCheckEvent = true;
                if (menus.Contains("[" + node.NodeID + "]"))
                {
                    node.Checked = true;
                }
                n1.Nodes.Add(node);
                if (childs > 0)
                {
                    ResolveSubTree(row.MenuID, node,menus);
                }
            }                                   
        }
        private void ResolveSubTree(int id, TreeNode treeNode,string ms)
        {
            List<SDLITS.Model.Menu> lists = areall.FindAll(p => p.ParentID == id);
            if (lists.Count > 0)
            {
                foreach (SDLITS.Model.Menu row in lists)
                {
                    TreeNode node = new TreeNode();
                    node.Text = row.MenuName;
                    node.NodeID = row.MenuID.ToString();
                    node.EnableCheckEvent = true;
                    node.EnableCheckBox = true;
                    if (ms.Contains("[" + node.NodeID + "]"))
                    {
                        node.Checked = true;
                    }
                    treeNode.Nodes.Add(node);
                    treeNode.Expanded = true;
                    int childs = areall.FindAll(p => p.ParentID == row.MenuID).Count;
                    if (childs>0)
                    {
                        ResolveSubTree(row.MenuID, node,ms);
                    }
                }
            }
        }

        protected void btnSaveRefresh_Click(object sender, EventArgs e)
        {
            int returnvalue = 0;
            Role role = new Role();
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                role.RoleID = int.Parse(Request["id"]);
                role.Attach();
                role.Description = txt_Descript.Text;
                role.RoleName = txt_Name.Text;
                returnvalue = SDDbSession.Default.Update<Role>(role);
                returnvalue=SDDbSession.Default.Delete<RoleInMenu>(RoleInMenu._.RoleID == Request["id"]);
                AddRoleMenu(int.Parse(Request["id"]));
            }
            else
            {
                ModelLoginUser user = Session["LoginUser"] as ModelLoginUser;
                role.DeAttach();
                role.CreateDate = DateTime.Now;
                role.CreateUserID = user.UserID;
                role.Description = txt_Descript.Text;
                role.RoleName = txt_Name.Text;
                returnvalue=SDDbSession.Default.Insert<Role>(role);
                AddRoleMenu(returnvalue);
            }
            if (returnvalue > 0)
            {
                PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
            }
        }
        protected void AddRoleMenu(int roleid)
        {
            foreach (TreeNode s in treeMenu.GetCheckedNodes())
            {
                if (!string.IsNullOrEmpty(s.NodeID) && s.NodeID != "0")
                {
                    RoleInMenu rim = new RoleInMenu();
                    rim.DeAttach();
                    rim.MenuID = int.Parse(s.NodeID);
                    rim.RoleID = roleid;
                    SDDbSession.Default.Insert<RoleInMenu>(rim);
                }
            }
        }
        protected void treeMenu_NodeCheck(object sender, TreeCheckEventArgs e)
        {
            if (e.Checked)
            {
                treeMenu.CheckAllNodes(e.Node.Nodes);
            }
            else
            {
                treeMenu.UncheckAllNodes(e.Node.Nodes);
            }
            if (e.Node.Leaf)
            {
                e.Node.ParentNode.Checked = true;
            }
        }
    }
}